package com.example.membership.service.impl;
import com.example.membership.dao.ProductDao;
import com.example.membership.model.Product;
import com.example.membership.service.ProductService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    private final ProductDao productDao;

    public ProductServiceImpl(ProductDao productDao) {
        this.productDao = productDao;
    }

    @Override
    public Product getProductById(int id) {
        return productDao.findById(id);
    }

    @Override
    public Map<String, Object> getAllProducts(String category, String sortBy, int page, int pageSize) {
        List<Product> products = productDao.findAll(category, sortBy, page, pageSize);
        int totalItems = productDao.countAll(category);
        Map<String, Object> response = new HashMap<>();
        response.put("items", products);
        response.put("totalItems", totalItems);
        return response;
    }

    @Override
    public void createProduct(Product product) {
        productDao.insert(product);
    }

    @Override
    public void updateProduct(Product product) {
        productDao.update(product);
    }

    @Override
    public void deleteProduct(int id) {
        productDao.delete(id);
    }
}